www.gusucode.com > VC VMR视频采集与显示程序-源码程序 > VC VMR视频采集与显示程序-源码程序/code/VMRDlg.cpp

    // VMRDlg.cpp : implementation file
// Download by http://www.NewXing.com
#include "stdafx.h"
#include "VMR.h"
#include "VMRDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define TIMER_ID 1
#define TIMER_DELAY 40
#define CAMERA_WIDTH  640	//352
#define CAMERA_HEIGHT 480	//288

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVMRDlg dialog

CVMRDlg::CVMRDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVMRDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVMRDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CVMRDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVMRDlg)
	DDX_Control(pDX, IDC_LISTDEVICE, m_listCtrl);
	DDX_Control(pDX, IDC_VIDEO_WINDOW, m_videoWindow);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CVMRDlg, CDialog)
	//{{AFX_MSG_MAP(CVMRDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_PREVIEW, OnPreview)
	ON_BN_CLICKED(ID_PAUSEPLAY, OnPauseplay)
	ON_BN_CLICKED(ID_CAPTURE, OnCapture)
	ON_BN_CLICKED(ID_STOPCAP, OnStopcap)
	ON_BN_CLICKED(ID_SAVEGRAPH, OnSavegraph)
	ON_BN_CLICKED(ID_EXITAPP, OnExitapp)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVMRDlg message handlers

BOOL CVMRDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	this->m_VMRCap.EnumDevices(this->m_listCtrl);
	this->m_listCtrl.SetCurSel(0);
	m_yuvFileName = _T("");
	m_fileState = FALSE;
	m_imageWidth = CAMERA_WIDTH;
	m_imageHeight= CAMERA_HEIGHT;
	p_yuv420 = NULL;

	m_tooltip.Create(this);
	m_tooltip.Activate(TRUE);
	m_tooltip.AddTool(GetDlgItem(ID_PREVIEW), _T("预览视频"));
	m_tooltip.AddTool(GetDlgItem(ID_PAUSEPLAY), _T("暂停/开始预览"));
	m_tooltip.AddTool(GetDlgItem(ID_CAPTURE), _T("图像捕捉"));
	m_tooltip.AddTool(GetDlgItem(ID_STOPCAP), _T("终止捕捉"));
	m_tooltip.AddTool(GetDlgItem(ID_SAVEGRAPH), _T("保存链表"));
	m_tooltip.AddTool(GetDlgItem(ID_EXITAPP), _T("退出应用程序"));
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CVMRDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CVMRDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CVMRDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CVMRDlg::OnPreview() 
{
	HWND hwnd = this->m_videoWindow.GetSafeHwnd();
	int id = this->m_listCtrl.GetCurSel();
	HRESULT hr = m_VMRCap.Init(id,hwnd,m_imageWidth,m_imageHeight);

//	this->m_VMRCap.SaveGraph( "c:\\xyc.grf" );

	if (FAILED(hr)) 
		AfxMessageBox(_T("无法创建滤波器链表!"));
	//m_VMRCap.PreviewImages(id , hVWindow);

	// TODO: Add your control notification handler code here
	
}

void CVMRDlg::OnPauseplay() 
{

	static int c=0;
	if (!c) {
		GetDlgItem(ID_PAUSEPLAY)->SetWindowText(_T("开始预览"));
		c = 1;
	} else {
		GetDlgItem(ID_PAUSEPLAY)->SetWindowText(_T("暂停预览"));
		c = 0;
	}
	m_VMRCap.Pause();
	
	// TODO: Add your control notification handler code here
	
}

void CVMRDlg::OnCapture() 
{
	CFileDialog dlg(FALSE);

	if (dlg.DoModal()==IDOK)
	{
		m_yuvFileName = dlg.GetPathName();
		BOOL ret = m_pFile.Open(m_yuvFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
		if (!ret)
		{
			AfxMessageBox(_T("创建YUV文件失败!"));
			return ;
		}
		else
			m_fileState = ret;
		if (!p_yuv420)
			p_yuv420 = new BYTE [m_imageWidth*m_imageHeight*3/2];
		this->KillTimer(TIMER_ID);
		this->SetTimer(TIMER_ID,TIMER_DELAY,NULL);
	}
	
	// TODO: Add your control notification handler code here
	
}

void CVMRDlg::OnStopcap() 
{
	
		KillTimer(TIMER_ID);
	if (p_yuv420)
	{
		delete [] p_yuv420;
		p_yuv420 = NULL;
	}
	if (m_fileState)
	{
		m_pFile.Close();
		m_fileState = FALSE;
	}
	// TODO: Add your control notification handler code here
	
}

void CVMRDlg::OnSavegraph() 
{

	HRESULT hr;
	CFileDialog dlg(FALSE);
	
	if (dlg.DoModal()==IDOK)
	{
		CString inFileName = dlg.GetPathName();
		m_VMRCap.SaveGraph(inFileName);
	}
	// TODO: Add your control notification handler code here
	
}

void CVMRDlg::OnExitapp() 
{

KillTimer(TIMER_ID);
	if (p_yuv420)
		delete [] p_yuv420;
	if (m_fileState)
		m_pFile.Close();
	CDialog::OnOK();
	// TODO: Add your control notification handler code here
	
}

BOOL CVMRDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	{
		// Let the ToolTip process this message.
		m_tooltip.RelayEvent(pMsg);
	}
	return CDialog::PreTranslateMessage(pMsg);
}

void CVMRDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
		if (nIDEvent==TIMER_ID)
	{	//定时读取获取图像帧,转换颜色空间,写文件
		if (m_fileState)
		{
			DWORD dwSize;
			dwSize = this->m_VMRCap.GrabFrame();
			if (dwSize >0 )
			{
				BYTE *pImage;
				this->m_VMRCap.GetFrame(&pImage);
				//conv.RGB24_to_YV12(pImage,this->p_yuv420,m_imageWidth,m_imageHeight);
				m_pFile.Write(p_yuv420,m_imageWidth*m_imageHeight*3/2);
				//m_pFile.Write(p_yuv420,m_imageWidth*m_imageHeight*3/2);
			}
		}
	}

	CDialog::OnTimer(nIDEvent);
}